
import {
    playBaiduAudio,
    getPlayList,
    controlPlay,
    control,
    getNewVersion,
    getVersion,
    upgradeDevice,
    upgradeStatus,
    searchAlbum,
    searchAudio,
    getTracklist,
    getTrack,
    audioCustomPlay
} from './baidu_api';
import { sendLogToEmail } from './logger';
import {
    API_CODE_SERVER_ERR
} from '../config/api_code';

/**
 * 操作BaiduApi接口
 */
class Baidu {
    constructor() {
        // 点播控制
        this.playBaiduAudio = this.playBaiduAudio.bind(this);
        this.getPlayList = this.getPlayList.bind(this);
        this.playControl = this.playControl.bind(this);
        this.control = this.control.bind(this);

        // 固件版本,升级操作
        this.getNewVersion = this.getNewVersion.bind(this);
        this.getVersion = this.getVersion.bind(this);
        this.upgradeDevice = this.upgradeDevice.bind(this);
        this.checkUpgrade = this.checkUpgrade.bind(this);

        // 获取专辑音频信息
        this.searchAlbum = this.searchAlbum.bind(this);
        this.searchAudio = this.searchAudio.bind(this);
        this.getTrackList = this.getTrackList.bind(this);
        this.getTrack = this.getTrack.bind(this);

        // 自定义播放
        this.customPlay = this.customPlay.bind(this);
    }

    // 点播百度音频
    async playBaiduAudio(req, res) {
        const { device_uuid, album_id, track_id, token } = req.body;
        try {
            const { err, result } = await playBaiduAudio(device_uuid, album_id, track_id, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result);
            const code = parseInt(json.code,10);
            if (code === 0 || code === 1005) {
                res.apiSuccess();
            }else {
                res.apiError(code,json);
            }
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(`playBaiduAudio -> device_uuid:${device_uuid},${album_id},${track_id},${token},${result},err:${err.stack}`);
        }
    }


    // 获取播放列表
    async getPlayList(req, res) {
        const { device_uuid, token } = req.body;
        try {
            const { err, result } = await getPlayList(device_uuid, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            console.log(result);
            const code = parseInt(result.status, 10);
            if (code === 0) {
                res.apiSuccess(result.data);
            } else {
                res.apiError(-1, result);
            }
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }


    // 控制接口: 控制播放暂停、继续、上一首/下一首 (NEXT、PREVIOUS、PLAY_PAUSE)
    async playControl(req, res) {
        const { device_uuid, action, token } = req.body;
        try {
            const { err, result } = await controlPlay(device_uuid, action, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }

            const json = JSON.parse(result)
            if (parseInt(json.code, 10) !== 0) {
                res.apiError(json.code,json);
                return;
            }
            res.apiSuccess(json);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 控制设备指令
    async control(req, res) {
        const { device_uuid, data, token } = req.body;
        try {
            if (!device_uuid) {
                res.apiError(API_CODE_SERVER_ERR,`token:${token},对应的uuid为空`);
                return;
            }
            const { err, result } = await control(device_uuid, data, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            res.apiSuccess(result);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }


    // 获取最新升级版本
    async getNewVersion(req, res) {
        const { device_uuid, token } = req.body;
        try {
            const { err, result } = await getNewVersion(device_uuid, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            // 该接口必须转换
            const data = JSON.parse(result);
            const code = parseInt(data.code);
            if (code === 0) {
                res.apiSuccess(data.data);
            } else {
                res.apiError(code, data);
            }
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }


    // 获取设备当前版本
    async getVersion(req, res) {
        const { device_uuid, token } = req.body;
        try {
            const { err, result } = await getVersion(device_uuid, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            // 该接口必须转换
            const data = JSON.parse(result);
            const code = parseInt(data.code);
            if (code === 0) {
                res.apiSuccess(data.data);
            } else {
                res.apiError(code, data);
            }

        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 通知设备升级
    async upgradeDevice(req, res) {
        const { device_uuid, package_id, strategy_id, token } = req.body;
        try {
            const { err, result } = await upgradeDevice(device_uuid, package_id, strategy_id, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            res.apiSuccess(result);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 检查升级状态
    async checkUpgrade(req, res) {
        const { device_uuid, token } = req.body;
        try {
            const { err, result } = await upgradeStatus(device_uuid, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result);
            res.apiSuccess(json.data);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }



    // 搜索专辑
    async searchAlbum(req, res) {
        const { device_uuid,tags,words, page, size, token } = req.body;
        try {
            const { err, result } = await searchAlbum(device_uuid,tags, words, page, size, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result);
            const code = parseInt(json.status ,10);
            if (code) {
                res.apiError(code, result);
                return;
            }
            res.apiSuccess(json.data.list);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 搜索音频
    async searchAudio(req, res) {
        const {device_uuid, tags, words, page, size, token } = req.body;
        try {
            const { err, result } = await searchAudio(device_uuid,tags, words, page, size, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result)
            const code = parseInt(json.status ,10);
            if (code) {
                res.apiError(code, result);
                return;
            }
            res.apiSuccess(json.data.list);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 获取专辑列表
    async getTrackList(req, res) {
        const { uuid, albumid, page, psize, upid, token } = req.body;
        try {
            const { err, result } = await getTracklist(uuid, albumid, page, psize, upid, token);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }

            const json = JSON.parse(result)
            const code = parseInt(json.status ,10);
            if (code) {
                res.apiError(code, json);
                return;
            }
            res.apiSuccess(json.data.list);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 获取单个音频信息
    async getTrack(req, res) {
        const { uuid, track_id, token } = req.body;
        try {
            const { err, result } = await getTrack(uuid, track_id, token)
            if (err) {
                res.apiError(API_CODE_SERVER_ERR,err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result)
            const code = parseInt(json.status ,10);
            if (code) {
                res.apiError(cod, result);
                return;
            }
            res.apiSuccess(json.data);
        } catch (err) {
            res.apiError(API_CODE_SERVER_ERR,err);
            sendLogToEmail(err);
        }
    }

    // 自定义播放
    async customPlay(req, res) {
        const { uuid, token, category_id, audio_id } = req.body;
        try {
            const { err, result } = await audioCustomPlay(uuid, token, category_id, audio_id);
            if (err) {
                res.apiError(API_CODE_SERVER_ERR, err);
                sendLogToEmail(err);
                return;
            }
            const json = JSON.parse(result);
            const code = parseInt(json.code, 10);
            if (code === 0 || code === 1005) {
                res.apiSuccess();
            } else {
                res.apiError(code, json);
            }
        } catch (error) {
            res.apiError(API_CODE_SERVER_ERR, error);
            sendLogToEmail(error);
        }
    }

}

export default new Baidu();